import requests
from bs4 import BeautifulSoup
# -*- encoding:utf-8 -*-
if __name__=='__main__':
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54'
    }
    url = 'https://www.shicimingju.com/book/hongloumeng.html'
    page_content=requests.get(url = url,headers=headers).content
    soup = BeautifulSoup(page_content,'lxml')
    li_list = soup.select('.book-mulu > ul > li')
    fp = open('./hongloumeng.txt','w',encoding='utf-8')
    for li in li_list:
        title = li.a.string
        detail_url = 'https://www.shicimingju.com'+li.a['href']
        detail_page_content = requests.get(url=detail_url,headers=headers).content
        detail_soup = BeautifulSoup(detail_page_content,'lxml')
        div_tag = detail_soup.find('div',class_='chapter_content')
        content = div_tag.text
        fp.write(title+':'+content+'/n')
        print(title,'over')

